import axios from 'axios'
axios.defaults.baseURL = 'http://klxin.cn:8888/api/private/v1/'
//axios.defaults.baseURL='http://127.0.0.1:8888/api/private/v1/'


//导入  加载效果
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  //console.log(config,5555);
  NProgress.start()
  var token = sessionStorage.getItem('token')
  config.headers.Authorization = token
  // 在发送请求之前做些什么

  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  NProgress.done()
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});



/* 1-实现登录 */
export var login_api = (params) => {
  return axios.post('login', params)
}

/* 2-左侧菜单权限 */
export var menus_api = () => {
  return axios.get('menus')
}

/* 3-用户数据列表 */
export var users_api = (params1) => {
  return axios.get('users', {
    params: params1
  })
}

/* 4-修改用户状态 */
export var usersState_api = (params) => {
  return axios.put(`users/${params.uId}/state/${params.type}`)
}
// usersState_api({uId:1,type:1})


/* 5-删除单个用户 */
export var usersDelete_api = (params) => {
  return axios.delete(`users/${params}`)
}
// usersDelete_api(2)

//  6-编辑用户提交
export var usersEdit_api = (params) => {
  return axios.put(`users/${params.id}`, {
    email: params.email,
    mobile: params.mobile
  })
}
//  usersEdit_api({id:1,email:111@qq.com,mobile:12333333})


//  7-添加用户
export var usersAdd_api = (params) => {
  return axios.post(`users/`, params)
}
//  usersAdd_api({username:1,password:12222,email:11@qq.com,mobile:1333333})

//  8-所有权限列表
export var rightsAll_api = (type) => {
  return axios.get(`rights/` + type)
}

//  9-角色管理 角色列表
export var rolesAll_api = () => {
  return axios.get(`roles/`)
}

//   10-0添加角色

export var rolesAdd_api = (params) => {
  return axios.post(`roles`, params)
}
//  11-角色授权
export var rolesToRights_api = (params) => {
  return axios.post(`roles/${params.roleId}/rights`, {
    rids: params.rids
  })
}
//  rolesToRights_api({roleId:1,rids:22})

//  12-角色列表
export var rolesList_api = (params) => {
  return axios.get(`roles`)
}

// 13-分配用户角色
export var rolesSet_api = (params) => {
  return axios.put(`users/${params.id}/role`, {
    rid: params.rid
  })
}
//  rolesList_api({id:1,rid:2})

//  14-分类数据列表获取    categories
export var cateList_api = (params) => {
  return axios.get(`categories`, {
    params
  })
}

//  cateList_api({type:3,page:1,pagesize:3})

//  15-添加分类
export var cateAdd_api = (params) => {
  return axios.post(`categories`, params)
}

//  16-参数列表  16-categories/:id/attributes
export var cateParamsList_api = (params1) => {
  return axios.get(`categories/${params1.id}/attributes`, {
    params: {
      sel: params1.sel
    }
  })
}

//  cateParamsList_api({id:1,sel:only})


//17-删除参数
export var cateParamsDelete_api = (params1) => {
  return axios.delete(`categories/${params1.id}/attributes/${params1.attrid}`)
}
//  cateParamsDelete_api({id:1,attrid:2})


//  18-编辑提交参数
// export var  cateAtteAdd_api=(params)=>{
//   return axios.put(`categories/${params.id}/attributes`,{
//     attr_name:params.attr_name,
//     attr_sel:params.attr_sel,
//     attr_vals:params.attr_vals
//   })
// }
export var cateAtteAdd_api = (params) => {
  return axios.put(`categories/${params.id}/attributes/${params.attrId}`, {
    attrId: params.attrId,
    attr_name: params.attr_name,
    attr_sel: params.attr_sel,
    attr_vals: params.attr_vals
  })
}
//  cateAtteAdd_api({id:1,attr_name:2,attr_sel:3,attr_vals:4})


//  19-添加动态参数或者静态属性

export var cateAddParams_api = (params) => {
  return axios.post(`categories/${params.id}/attributes`, {
    attr_name: params.attr_name,
    attr_sel: params.attr_sel,
    attr_vals: params.attr_vals
  })
}

//  20-商品列表
export var goodrs_api = (params1) => {
  return axios.get('goods', {
    params: params1
  })
}

//  21--删除商品
export var goodsDelete_api = (params) => {
  return axios.delete(`goods/${params}`)
}

//  22--添加商品
export var goodsAdd_api = (params) => {
  return axios.post(`goods`, params)
}
//  23-订单数据列表
export var orders_api = (params) => {
  return axios.get(`orders`, { params })
}

//  24 reports/type/1
export var reports_api = (params) => {
  return axios.get(`reports/type/1`)
}